package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.SimpleAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.SpEmergencyReportCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpEmergencyReportResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpEmergencyReportUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpEmergencyReportEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface SpEmergencyReportConvert {
    SpEmergencyReportConvert INSTANCE = Mappers.getMapper(SpEmergencyReportConvert.class);

    SpEmergencyReportEntity createTo(SpEmergencyReportCreateDTO s);

    SpEmergencyReportEntity updateTo(SpEmergencyReportUpdateDTO s);

    SpEmergencyReportResultDTO convertTo(SpEmergencyReportEntity s);

    default SpEmergencyReportEntity create(SpEmergencyReportCreateDTO s, Long tenantId, SimpleAuditActionStatusEnum action) {
        SpEmergencyReportEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setPreAction(action);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default SpEmergencyReportEntity update(SpEmergencyReportUpdateDTO s) {
        SpEmergencyReportEntity entity = updateTo(s);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default SpEmergencyReportResultDTO convert(SpEmergencyReportEntity s) {
        SpEmergencyReportResultDTO result = convertTo(s);
        renderAreaList(result);
        return result;
    }

    default void additionalArea(SpEmergencyReportEntity s, List<String> areaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(areaList)) {
            return;
        }
        s.setFsszs(areaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setFsszds(areaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (areaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setFsszxq(areaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setFsszxq(null);
        }

        if (areaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setFsszd(areaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setFsszd(null);
        }
    }

    default void renderAreaList(SpEmergencyReportResultDTO s) {
        if (Objects.isNull(s)) {
            return;
        }

        List<String> residenceAreaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getFsszs()), () -> residenceAreaList.add(s.getFsszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getFsszds()), () -> residenceAreaList.add(s.getFsszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getFsszxq()), () -> residenceAreaList.add(s.getFsszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getFsszd()), () -> residenceAreaList.add(s.getFsszd()));

        s.setAreaList(residenceAreaList);
    }
}
